package cn.itcast.service.impl;

import cn.itcast.clients.OrderClients;
import cn.itcast.clients.UserClients;
import cn.itcast.entity.*;
import cn.itcast.mapper.RoomMapper;
import cn.itcast.DTO.OrderDTO;
import cn.itcast.DTO.PageQueryDTO;
import cn.itcast.pojo.entity.*;
import cn.itcast.service.RoomService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RoomServiceImpl implements RoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private OrderClients orderClients;
    @Autowired
    private UserClients userClients;

    //条件分页查询
    @Override
    public PageResult page(PageQueryDTO dto) {
        //设置分页参数
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        //调用roomMapper的page方法查询分页数据
        List<Room> list = roomMapper.page(dto);
        //将list转换为Page对象
        Page<Room> page = (Page<Room>) list;
        //创建PageResult对象并返回
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    //新增
    @Override
    public void insert(Room room) {
        roomMapper.insert(room);
    }

    //删除
    @Override
    public void delete(Long id) {
        roomMapper.delete(id);
    }

    //根据id查询
    @Override
    public Room findById(Long id) {
        return roomMapper.findById(id);
    }

    //修改房间信息
    @Override
    public void update(Room room) {
        roomMapper.update(room);
    }

    //查询所有
    @Override
    public List<Room> selectAll() {
        return roomMapper.selectAll();
    }

    //根据房间号查id
    @Override
    public Integer findIdByRoomNo(String roomNo) {
        Integer id = roomMapper.findIdByRoomNo(roomNo);
        return id;
    }

    @Override
    public OrderDTO findOrderById(Long id) {
        Room room = new Room();
        room.setId(id);
        // 使用orderClients对象调用getOrderById方法，获取指定房间的订单信息
        Result<Order> orderById = orderClients.getOrderById(room);
        Order order = orderById.getData();
        log.info("order:{}", order);
        // 使用userId获取User对象
        Result<User> byId = userClients.findById(order.getUserId());
        User user = byId.getData();
        log.info("user:{}", user);
        // 创建一个OrderDTO对象，并将order和user的属性复制到OrderDTO对象中
        OrderDTO orderDTO = new OrderDTO();
        BeanUtils.copyProperties(order, orderDTO);
        BeanUtils.copyProperties(user, orderDTO);
        // 返回OrderDTO对象
        return orderDTO;
    }


}
